package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.config.ThreadPoolExecutorConfiguration;
import com.atguigu.tingshu.search.repository.AlbumIndexRepository;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumInfoDetailVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumIndexRepository albumIndexRepository;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;



    @Override
    public AlbumInfoDetailVo getAlbumInfoDetail(Long albumId) {
        AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        if(!bloomFilter.contains(albumId)){
//            throw new GuiguException(ResultCodeEnum.FIND_DATA_ERROR);
//        }


        //获取专辑信息
        CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(albumId).getData();
            albumInfoDetailVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, threadPoolExecutor);

        //获取专辑统计数据信息
        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumInfoFeignClient.getAlbumStatVo(albumId).getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVo);
        },threadPoolExecutor);

        //通过三级分类id获取分类信息
        CompletableFuture<Void> cf3 = cf1.thenAcceptAsync(albumInfo -> {
            albumInfoDetailVo.setBaseCategoryView(categoryFeignClient.findCategoryInfoByC3Id(albumInfo.getCategory3Id()).getData());
        }, threadPoolExecutor);

        //通过用户Id 获取到主播信息
        CompletableFuture<Void> cf4 = cf1.thenAcceptAsync(albumInfo -> {
            UserInfo userInfo = userInfoFeignClient.getUserInfoById(albumInfo.getUserId()).getData();
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            albumInfoDetailVo.setAnnouncer(userInfoVo);
        }, threadPoolExecutor);

        //阻塞线程
        CompletableFuture.allOf(cf1, cf2, cf3, cf4).join();

        return albumInfoDetailVo;
    }


}
